//
//  TZWMainTabBar.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/8.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

import UIKit

class TZWMainTabBar: UITabBar {

// 1 定义的闭包属性，传递
    var composeClosure:(() -> ())?
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //宽度
        let width = self.frame.size.width / CGFloat(HMMainTabBarButtonCount)
        //创建 frame
        let newFrame = CGRect(x: 0, y: 0, width: width, height:  frame.size.height)
        var index = 0
        for subView in subviews {
            if subView.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subView.frame = CGRectOffset(newFrame, CGFloat(index) * width, 0)
                index += index == 1 ? 2 : 1
            }
            
        }
        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: frame.size.height)
        
    }
    
    //MARK: 按钮点击事件
//    @objc: 让oc 调用我们的方法，系统默认会认为所有的swift 方法加上@objc。如果方法中加了private。就不会帮我们加入@objc
    @objc private func didClickComposeButton() {
        composeClosure?()
    }
    
    //MARK: 懒加载
//    加号的加载
    private lazy var composeButton: UIButton = {
        let button = UIButton()
        //设置
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        
        button.addTarget(self, action: Selector("didClickComposeButton"), forControlEvents: UIControlEvents.TouchUpInside)
        
        //添加按钮到tabBar
//        self.addSubView(button)
        
        self.addSubview(button)
        
        return button
        
    }()
    
}
